package com.gege.hmily.order.controller;

import com.gege.hmily.order.model.Order;
import com.gege.hmily.order.service.OrderService;
import com.gege.hmily.response.BaseResponse;
import com.gege.hmily.response.DataResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author gechenpeng
 * @date 2021-05-28
 */
@Api(tags = "用户")
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    private final OrderService orderService;

    @Autowired(required = false)
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @ApiOperation("下单")
    @PostMapping("/take")
    public DataResponse<Order> takeOrder(@RequestParam(value = "amount") BigDecimal amount) {
        Order order = orderService.saveOrder(Order.builder()
                .totalAmount(amount)
                .build());
        return DataResponse.ok(order);
    }

    @ApiOperation("支付")
    @PostMapping("/pay")
    public BaseResponse pay(@RequestParam(value = "orderId") Long orderId) {
        orderService.pay(orderId);
        return BaseResponse.ok();
    }
}
